package com.example.dellpc.myapplication;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.NotificationCompat;
import android.widget.Button;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnChildClick;
import com.lidroid.xutils.view.annotation.event.OnClick;

/**
 * Created by 刘智强 on 2016/9/23.
 */
public class NotificationActivity extends Activity {

    private NotificationManager notificationManager;
    private Notification notification;
    private NotificationCompat.Builder builder;
    private final int REQUEST_CODE = 0xb01;
    @ViewInject(R.id.btn_send)
    private  Button btn_send;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);
        ViewUtils.inject(this);
        init();
    }

    public void init() {


//        initNotification();


    }
    @OnClick(R.id.btn_send)
    public  void onClick(Button button){
        int id=button.getId();
        switch (id){

            case R.id.btn_send:
                sendNotification(this,1,"测试","测试信息");
                break;
            default:
        }

    }

    public void initNotification() {
        notificationManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
        builder = new NotificationCompat.Builder(this);
        builder.setContentTitle("测试通知")
                .setContentText("测试信息!!!")
                .setSmallIcon(R.mipmap.bird)
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL))
                .setTicker("测试");

        notification = builder.build();
        notificationManager.notify(1, notification);

    }

    public PendingIntent getDefalutIntent(int flags) {
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, new Intent(), flags);
        return pendingIntent;
    }

    private void sendNotification(Context context, int notification_ID, String title, String content) {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        //使用默认的通知栏布局

        Notification.Builder builder = new Notification.Builder(context);
        // 此处设置的图标仅用于显示新提醒时候出现在设备的通知栏
        builder.setSmallIcon(R.drawable.ic_launcher);
        builder.setContentTitle(title);
        builder.setContentText(content);

        Notification notification = builder.build();

                 /*    使用自定义的通知栏布局
 80          *  当用户下来通知栏时候看到的就是RemoteViews中自定义的Notification布局
 81          */
        // RemoteViews contentView = new RemoteViews(context.getPackageName(),
        // R.layout.notification);
        // contentView.setImageViewResource(R.id.imageView, R.drawable.ic_launcher);
        // contentView.setTextViewText(R.id.title, "土耳其和IS的秘密");
        // contentView.setTextViewText(R.id.text, "土耳其拒绝向俄罗斯道歉，怀疑有IS撑腰");
        // notification.contentView = contentView;

        // 发送通知到通知栏时：提示声音 + 手机震动 + 点亮Android手机呼吸灯。
        // 注意！！（提示声音 + 手机震动）这两项基本上Android手机均支持。
        // 但Android呼吸灯能否点亮则取决于各个手机硬件制造商自家的设置。
        notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS;

        // 点击notification自动消失
        notification.flags = Notification.FLAG_AUTO_CANCEL;

        // 通知的时间
        notification.when = System.currentTimeMillis();

        // 需要注意的是，作为选项，此处可以设置MainActivity的启动模式为singleTop，避免重复新建onCreate()。
        Intent intent = new Intent(context, NotificationActivity.class);

        // 当用户点击通知栏的Notification时候，切换回MainActivity。
        PendingIntent pi = PendingIntent.getActivity(context, REQUEST_CODE, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        notification.contentIntent = pi;

        // 发送到手机的通知栏
        notificationManager.notify(notification_ID, notification);
    }
}
